package com.happy.second.Service.impl;

import com.happy.second.Entity.qlpEntity.User;
import com.happy.second.Entity.ynnEntity.Product;
import com.happy.second.Mapper.QlpMapper.UserMapper;
import com.happy.second.Mapper.YnnMapper.ProductMapper;
import com.happy.second.Service.UserService;
import com.happy.second.Utils.Exception.RuntimeException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author itoutsource.cz10
 */
@Service(value = "UserService")
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private ProductMapper productMapper;

    @Override
    public User selectByPrimaryQlp(int id) {
        return userMapper.selectByUserId(id);
    }

    @Override
    public Product selectByPrimaryYnn(int id) {
        return productMapper.selectByProductId(id);
    }

    @Override
    public int insertIntoUsers(List<User> users) {
        return userMapper.insertIntoUser(users);
    }

    /**
     * AOP事务默认只对RuntimeException异常回滚
     * 多数据源 指定对应的事务
     **/
    @Transactional(value = "prodTransactionManager",rollbackFor = RuntimeException.class)
    @Override
    public void insertUser(List<User> users) {
        try {
            for (int i = 0; i < users.size(); i++) {
                userMapper.insertUser(users.get(i));
            }
        }catch (Exception e){
            throw new RuntimeException("异常！");
        }

    }
}
